/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * @author Stephane
 */
package DAO;

import ObjetData.LiensPhotos;

import java.util.HashMap;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Steph
 */
public class DaoLiensPhotos extends DAO<LiensPhotos> {

    private HashMap<Integer, LiensPhotos> liensPhotos = new HashMap<Integer, LiensPhotos>();
    private Integer lastId;

    @Override
    public HashMap<Integer, LiensPhotos> getAll() {
        try {
            ResultSet result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT ID_LIEN, ID_ANIMAL, LIEN FROM APP.LIENSPHOTOS");
            while (result.next()) {
                LiensPhotos lien = new LiensPhotos(result.getInt(1));
                lien.setIdAnimal(result.getInt("ID_ANIMAL"));
                lien.setLien(result.getString("LIEN"));
                liensPhotos.put(result.getInt(1), lien);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoLiensPhotos.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this.liensPhotos;
    }

    @Override
    public boolean delete(LiensPhotos obj) {
        try {
            Statement stmt = Connect.getConnexion().createStatement();
            String delete = "DELETE FROM APP.LIENSPHOTOS WHERE ID_LIEN =" + obj.getIdLien();
            stmt.executeUpdate(delete);
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean save(LiensPhotos obj) {

        try {
            Statement stmt = Connect.getConnexion().createStatement();
            String update = "UPDATE APP.LIENSPHOTOS SET LIEN='" + obj.getLien() + "' WHERE ID_LIEN =" + obj.getIdLien();
            stmt.executeUpdate(update);
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public Integer create(LiensPhotos obj) {

        try {
            Statement stmt = Connect.getConnexion().createStatement();
            Integer id = getLastId() + 1;
            String insert = "INSERT INTO APP.LIENSPHOTOS VALUES (" + id + "," + obj.getIdAnimal() + ",'" + obj.getLien() + "')";
            stmt.executeUpdate(insert);
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this.lastId + 1;
    }

    @Override
    public LiensPhotos find(int id) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if (id > 0) {
            LiensPhotos lien = new LiensPhotos();
            ResultSet result = null;
            try {
                result = Connect.getConnexion().createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_UPDATABLE).executeQuery(
                        "SELECT ID_LIEN, ID_ANIMAL, LIEN FROM APP.LIENSPHOTOS WHERE ID_LIEN = " + id);
                if (result.first()) {
                    result.first();
                    lien.setIdLien(result.getInt("ID_LIEN"));
                    lien.setIdAnimal(result.getInt("ID_ANIMAL"));
                    lien.setLien(result.getString("LIEN"));
                }
            } catch (SQLException ex) {
                Logger.getLogger(DaoLiensPhotos.class.getName()).log(Level.SEVERE, null, ex);
            }
            return lien;
        } else {
            throw new DaoException("Lien d'id [" + id + "] inconnu", 2);
        }
    }

    @Override
    public HashMap<Integer, LiensPhotos> search(String searchValue, Integer idCatValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public HashMap<Integer, LiensPhotos> getAllForAnimal(Integer idA) {
        if (idA > 0) {
            try {
                ResultSet result = Connect.getConnexion().createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY).executeQuery(
                        "SELECT ID_LIEN, ID_ANIMAL, LIEN FROM APP.LIENSPHOTOS WHERE ID_ANIMAL=" + idA);
                while (result.next()) {
                    LiensPhotos lien = new LiensPhotos(result.getInt(1));
                    lien.setIdAnimal(result.getInt("ID_ANIMAL"));
                    lien.setLien(result.getString("LIEN"));
                    liensPhotos.put(result.getInt(1), lien);
                }
            } catch (SQLException ex) {
                Logger.getLogger(DaoLiensPhotos.class.getName()).log(Level.SEVERE, null, ex);
            }
            return this.liensPhotos;
        } else {
            throw new DaoException("Lien Animal d'id [" + idA + "] inconnu", 2);
        }
    }

    private Integer getLastId() {
        ResultSet result = null;
        try {
            result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT MAX(ID_LIEN) as ID_LIEN FROM APP.LIENSPHOTOS");
            if (result.first()) {
                result.first();
                this.lastId = result.getInt("ID_LIEN");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
            this.lastId = 0;
        }
        return this.lastId;

    }
}